package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.config.IniConfig;
import com.js.rtu.util.BleUtils;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.ToastUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 25设置通信口参数
 * Created by zwz on 2020/6/22.
 */
public class Cmd_25Fragment extends BaseFragment {


    @BindView(R.id.sp_id)
    Spinner spId;
    @BindView(R.id.edt_address)
    EditText edtAddress;
    @BindView(R.id.sp_xieyi)
    Spinner spXieyi;
    @BindView(R.id.sp_power)
    Spinner spPower;
    @BindView(R.id.edt_cycle)
    EditText edtCycle;
    @BindView(R.id.edt_time)
    EditText edtTime;
    @BindView(R.id.sp_baud_rate)
    Spinner spBaudRate;
    @BindView(R.id.sp_stop_bit)
    Spinner spStopBit;
    @BindView(R.id.sp_data_bit)
    Spinner spDataBit;
    @BindView(R.id.sp_check_bit)
    Spinner spCheckBit;
    @BindView(R.id.cb_enalbe)
    CheckBox cbEnalbe;
    @BindView(R.id.edt_com)
    EditText edtCom;


    public static Cmd_25Fragment newInstance() {
        Bundle args = new Bundle();
        Cmd_25Fragment fragment = new Cmd_25Fragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_25;
    }

    @Override
    protected void initView() {

        String[] ids = {"1", "2", "3", "4", "5"};
        ArrayAdapter<String> mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, ids);
        spId.setAdapter(mAdapter);

        mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.agreement_data);
        spXieyi.setAdapter(mAdapter);

        mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.power_des);
        spPower.setAdapter(mAdapter);

        mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.baudRate_des);
        spBaudRate.setAdapter(mAdapter);

        mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.stopBit_des);
        spStopBit.setAdapter(mAdapter);

        mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.dataBit_des);
        spDataBit.setAdapter(mAdapter);


        mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.checkBit_des);
        spCheckBit.setAdapter(mAdapter);


    }





    private void set() {
        String com = edtCom.getText().toString();
        String cycle = edtCycle.getText().toString();
        String time = edtTime.getText().toString();
        String address = edtAddress.getText().toString();
        if (com.isEmpty() || cycle.isEmpty() || time.isEmpty() | address.isEmpty()) {
            toastShow("以上参数不能为空");
            return;
        }
        com = String.format(Locale.CHINA, "%02d", Integer.parseInt(com));


        String id = String.format(Locale.CHINA, "%02d", Integer.parseInt(spId.getSelectedItem().toString()));
        int ag = spXieyi.getSelectedItemPosition();//协议
        ag++;
        String agreement = String.format(Locale.CHINA, "%02x", ag);


//        String power = String.format(Locale.CHINA, "%s",
//                Integer.parseInt(IniConfig.power_settings.get(spPower.getSelectedItemPosition())));

        int powerPos = spPower.getSelectedItemPosition();
        powerPos=powerPos+1;//代号从1开始
        String power="1"+powerPos;//XY格式，X代表路数，Y代表电压的序号，目前固定路数为1


        address = String.format(Locale.CHINA, "%08d",
                Integer.parseInt(edtAddress.getText().toString()));

        cycle = String.format(Locale.CHINA, "%04x",
                Integer.parseInt(edtCycle.getText().toString()));


        String baudRate = String.format(Locale.CHINA, "%02d",
                Integer.parseInt(IniConfig.baudRate_data.get(spBaudRate.getSelectedItemPosition())));

        String stopBit = String.format(Locale.CHINA, "%02d",
                Integer.parseInt(IniConfig.stopBit_data.get(spStopBit.getSelectedItemPosition())));

        String dataBit = String.format(Locale.CHINA, "%02d",
                Integer.parseInt(IniConfig.dataBit_data.get(spDataBit.getSelectedItemPosition())));

        String checkBit = String.format(Locale.CHINA, "%02d",
                Integer.parseInt(IniConfig.checkBit_data.get(spCheckBit.getSelectedItemPosition())));

        String enable = cbEnalbe.isChecked() ? "01" : "00";

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder
                .append("01").append(" ")//保留字节
                .append(com).append(" ")//通信口
                .append(id).append(" ")//表计ID
                .append(enable).append(" ")//使能
                .append(agreement).append(" ")//协议
                .append(baudRate).append(" ")//波特率
                .append(dataBit).append(" ")//数据位
                .append(stopBit).append(" ")//停止位
                .append(checkBit).append(" ")//检验位
                //地址
                .append(address.substring(6, 8)).append(" ")
                .append(address.substring(4, 6)).append(" ")
                .append(address.substring(2, 4)).append(" ")
                .append(address.substring(0, 2)).append(" ")
                .append(BleUtils.integerToHexString(Integer.parseInt(time))).append(" ")//供电时间
//                .append(BleUtils.integerToHexString(Integer.parseInt(power))).append(" ")//电压
                .append(power).append(" ")//电压
                //周期
                .append(cycle.substring(2, 4)).append(" ")
                .append(cycle.substring(0, 2));
        sendData(stringBuilder.toString());
    }


    private void sendData(String data) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_25, data);

        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();
                String[] data = msg.split(" ");
                if (data.length < 18) {
                    ToastUtil.info(msg);
                    return;
                }
                if (data[12].equals(CmdConfig.CMD_25)) {
                    if (data[15].equals(CmdConfig.SUCCECC)) {
                        ToastUtil.success("设置成功");
                    } else {
                        ToastUtil.error("设置失败");
                    }

                }


            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }

    @OnClick(R.id.btn_set)
    public void onViewClicked() {
        set();
    }

}
